﻿using System;
using System.Data;

namespace DBMock.Validators
{
    /// <summary>
    /// 设置单元格为空字符串
    /// </summary>
    public class StringEmptyValidator : Validator
    {
        /// <summary>
        /// 设置为空字符串的情况
        /// </summary>
        public StringEmptyCase Case { get; set; }

        /// <summary>
        /// 指定值
        /// </summary>
        public object Value { get; set; }

        /// <summary>
        /// 初始化StringEmptyValidator
        /// </summary>
        /// <param name="emptyCase">设置为空字符串的情况</param>
        /// <param name="value">指定值</param>
        public StringEmptyValidator(StringEmptyCase emptyCase, object value)
        {
            Case = emptyCase;
            Value = value;
        }

        public override object Validate(object value, Strategy strategy, Builder[] group, DataRow row)
        {
            switch (Case)
            {
                case StringEmptyCase.Null:
                    if (value == null || value == DBNull.Value)
                        return string.Empty;

                    break;

                case StringEmptyCase.SpecifiedValue:
                    if (value == Value) { return string.Empty; }
                    break;

                case StringEmptyCase.NotSpecifiedValue:
                    if (value != Value) { return string.Empty; }
                    break;

                default:
                    break;
            }

            return value;
        }
    }

    /// <summary>
    /// 设置为空字符串的情况
    /// </summary>
    public enum StringEmptyCase
    {
        Null, //等于null
        SpecifiedValue, //等于指定值
        NotSpecifiedValue //不等于指定值
    }
}
